package com.quanqiutong.sunmi_printer.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.RemoteException;


import com.sunmi.peripheral.printer.ExceptionConst;
import com.sunmi.peripheral.printer.InnerPrinterCallback;
import com.sunmi.peripheral.printer.InnerPrinterException;
import com.sunmi.peripheral.printer.InnerPrinterManager;
import com.sunmi.peripheral.printer.InnerResultCallback;
import com.sunmi.peripheral.printer.SunmiPrinterService;
import com.sunmi.peripheral.printer.WoyouConsts;

public class SunmiPrintHelper {

    public static int NoSunmiPrinter = 0x00000000;
    public static int CheckSunmiPrinter = 0x00000001;
    public static int FoundSunmiPrinter = 0x00000002;
    public static int LostSunmiPrinter = 0x00000003;

    /**
     *  sunmiPrinter means checking the printer connection status
     */
    public int sunmiPrinter = CheckSunmiPrinter;
    /**
     *  SunmiPrinterService for API
     */
    private SunmiPrinterService sunmiPrinterService;

    private static SunmiPrintHelper helper = new SunmiPrintHelper();

    private SunmiPrintHelper() {}

    public static SunmiPrintHelper getInstance() {
        return helper;
    }

    private InnerPrinterCallback innerPrinterCallback = new InnerPrinterCallback() {
        @Override
        protected void onConnected(SunmiPrinterService service) {
            sunmiPrinterService = service;
            checkSunmiPrinterService(service);
        }

        @Override
        protected void onDisconnected() {
            sunmiPrinterService = null;
            sunmiPrinter = LostSunmiPrinter;
        }
    };

    public void initSunmiPrinterService(Context context){
        try {
            boolean ret =  InnerPrinterManager.getInstance().bindService(context,
                    innerPrinterCallback);
            if(!ret){
                sunmiPrinter = NoSunmiPrinter;
            }
        } catch (InnerPrinterException e) {
            e.printStackTrace();
        }
    }

    public void deInitSunmiPrinterService(Context context){
        try {
            if(sunmiPrinterService != null){
                InnerPrinterManager.getInstance().unBindService(context, innerPrinterCallback);
                sunmiPrinterService = null;
                sunmiPrinter = LostSunmiPrinter;
            }
        } catch (InnerPrinterException e) {
            e.printStackTrace();
        }
    }

    /**
     * Check the printer connection,
     * like some devices do not have a printer but need to be connected to the cash drawer through a print service
     */
    private void checkSunmiPrinterService(SunmiPrinterService service){
        boolean ret = false;
        try {
            ret = InnerPrinterManager.getInstance().hasPrinter(service);
        } catch (InnerPrinterException e) {
            e.printStackTrace();
        }
        sunmiPrinter = ret?FoundSunmiPrinter:NoSunmiPrinter;
    }

    private void handleRemoteException(RemoteException e){
    }

    public void sendRawData(byte[] data) {
        if(sunmiPrinterService == null){
            return;
        }
        try {
            sunmiPrinterService.sendRAWData(data, null);
        } catch (RemoteException e) {
            handleRemoteException(e);
        }
    }

    public void cutPaper(){
        if(sunmiPrinterService == null){
            return;
        }
        try {
            sunmiPrinterService.cutPaper(null);
        } catch (RemoteException e) {
            handleRemoteException(e);
        }
    }

    public void printerInit(){
        if(sunmiPrinterService == null){
            return;
        }
        try {
            sunmiPrinterService.printerInit(null);
        } catch (RemoteException e) {
            handleRemoteException(e);
        }
    }

    public void lineWrap(int n){
        if(sunmiPrinterService == null){
            return;
        }
        try {
            sunmiPrinterService.lineWrap(n, null);
        } catch (RemoteException e) {
            handleRemoteException(e);
        }
    }

    public String getPrinterSerialNo(){
        if(sunmiPrinterService == null){
            return "";
        }
        try {
            return sunmiPrinterService.getPrinterSerialNo();
        } catch (RemoteException e) {
            handleRemoteException(e);
            return "";
        }
    }

    public String getPrinterModal(){
        if(sunmiPrinterService == null){
            return "";
        }
        try {
            return sunmiPrinterService.getPrinterModal();
        } catch (RemoteException e) {
            handleRemoteException(e);
            return "";
        }
    }

    public String getPrinterVersion(){
        if(sunmiPrinterService == null){
            return "";
        }
        try {
            return sunmiPrinterService.getPrinterVersion();
        } catch (RemoteException e) {
            handleRemoteException(e);
            return "";
        }
    }

    public String getPrinterPaper(){
        if(sunmiPrinterService == null){
            return "";
        }
        try {
            return sunmiPrinterService.getPrinterPaper() == 1?"58mm":"80mm";
        } catch (RemoteException e) {
            handleRemoteException(e);
            return "";
        }
    }

    public void printerSelfChecking(){
        if(sunmiPrinterService == null){
            return;
        }
        try {
            sunmiPrinterService.printerSelfChecking(null);
        } catch (RemoteException e) {
            handleRemoteException(e);
        }
    }

    public void setAlignment(int align){
        if(sunmiPrinterService == null){
            return;
        }
        try {
            sunmiPrinterService.setAlignment(align, null);
        } catch (RemoteException e) {
            handleRemoteException(e);
        }
    }

    public void setFontSize(float fontsize){
        if(sunmiPrinterService == null){
            return;
        }
        try {
            sunmiPrinterService.setFontSize(fontsize, null);
        } catch (RemoteException e) {
            handleRemoteException(e);
        }
    }

    public void setPrinterStyle(int key, int value){
        if(sunmiPrinterService == null){
            return;
        }
        try {
            sunmiPrinterService.setPrinterStyle(key, value);
        } catch (RemoteException e) {
            handleRemoteException(e);
        }
    }

    public void printText(String text) {
        if(sunmiPrinterService == null){
            return;
        }
        try {
            sunmiPrinterService.printText(text, null);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void printTextWithFont(String text, String typeface, float fontsize) {
        if(sunmiPrinterService == null){
            return;
        }
        try {
            sunmiPrinterService.printTextWithFont(text, typeface, fontsize, null);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void printOriginalText(String text) {
        if(sunmiPrinterService == null){
            return;
        }
        try {
            sunmiPrinterService.printOriginalText(text, null);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void printColumnsText(String[] colsTextArr, int[] colsWidthArr, int[] colsAlign) {
        if(sunmiPrinterService == null){
            return;
        }
        try {
            sunmiPrinterService.printColumnsText(colsTextArr,colsWidthArr,colsAlign, null);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void printColumnsString(String[] colsTextArr, int[] colsWidthArr, int[] colsAlign) {
        if(sunmiPrinterService == null){
            return;
        }
        try {
            sunmiPrinterService.printColumnsString(colsTextArr,colsWidthArr,colsAlign, null);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void printBarCode(String data, int symbology, int height, int width, int textposition) {
        if(sunmiPrinterService == null){
            return;
        }
        try {
            sunmiPrinterService.printBarCode(data, symbology, height, width, textposition, null);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void printQRCode(String data, int modulesize, int errorlevel) {
        if(sunmiPrinterService == null){
            return;
        }
        try {
            sunmiPrinterService.printQRCode(data, modulesize, errorlevel, null);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void printQRCodeImage(String data, int format, int width, int height) {
        if(sunmiPrinterService == null){
            return;
        }
        try {
            Bitmap bitmap = BitmapUtil.generateBitmap(data, format, width, height);
            sunmiPrinterService.printBitmap(bitmap, null);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void printBitmap(byte[] bytes) {
        if(sunmiPrinterService == null){
            return;
        }
        try {
            Bitmap bitmap = null;
            if (bytes.length != 0) {
                bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
            }
            sunmiPrinterService.printBitmap(bitmap, null);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public int updatePrinterState(){
        if(sunmiPrinterService == null){
            return 0;
        }
        try {
            return sunmiPrinterService.updatePrinterState();
        } catch (RemoteException e) {
            e.printStackTrace();
            return 0;
        }
    }

    public void labelLocate() {
        if(sunmiPrinterService == null){
            return ;
        }
        try {
            sunmiPrinterService.labelLocate();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public void labelOutput() {
        if(sunmiPrinterService == null){
            return ;
        }
        try {
            sunmiPrinterService.labelOutput();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}
